import {Component, OnInit} from '@angular/core';
import {ModelBasePriceService} from '../model-base-price.service';
import {DatePipe} from '@angular/common';
import {LoveRecycManageService} from '../../love-recyc-manage.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {Router} from '@angular/router';

@Component({
  selector: 'app-model-base-price',
  templateUrl: './model-base-price.component.html',
  styleUrls: ['./model-base-price.component.scss']
})
export class ModelBasePriceComponent implements OnInit {
  query = {
    brandId: '',
    category: '',
    seriesId: '',
    modelId: '',
    time: [],
    page: 1,
    pageSize: 10
  };
  tableData = {
    records: [],
    total: 0
  };
  brandOptions: any = [];
  seriesOptions: any = [];
  modelOptions: any = [];

  constructor(
    private modelBasePriceService: ModelBasePriceService,
    private datePipe: DatePipe,
    private loveRecycManageService: LoveRecycManageService,
    private nzMessageService: NzMessageService,
    private router: Router,
  ) {
  }

  ngOnInit(): void {
    this.getTableData();
    this.loveRecycManageService.getRecycleBrandList({page: 1, pageSize: 99999}).then((res: any) => {
      this.brandOptions = res.data.records;
    });
  }

  brandChange() {
    this.query.seriesId = '';
    this.query.modelId = '';
    this.seriesOptions = [];
    this.modelOptions = [];
    this.getSeriesOptions();
  }

  categoryChange() {
    this.query.seriesId = '';
    this.query.modelId = '';
    this.seriesOptions = [];
    this.modelOptions = [];
    this.getSeriesOptions();
  }

  seriesChange() {
    this.query.modelId = '';
    this.modelOptions = [];
    this.getModelOptions();
  }

  getSeriesOptions() {
    this.loveRecycManageService.getRecycleSeriesList({
      brandId: this.query.brandId,
      category: this.query.category,
      page: 1,
      pageSize: 99999
    }).then((res: any) => {
      this.seriesOptions = res.data.records;
    });
  }

  getModelOptions() {
    this.loveRecycManageService.getRecycleModelList({
      brandId: this.query.brandId,
      category: this.query.category,
      seriesId: this.query.seriesId,
      page: 1,
      pageSize: 99999
    }).then((res: any) => {
      this.modelOptions = res.data.records;
    });
  }

  onPageIndexChange(index: number) {
    this.query.page = index;
    this.getTableData();
  }

  onPageSizeChange(pageSize: number) {
    this.query.pageSize = pageSize;
    this.getTableData();
  }

  queryBtn() {
    this.query.page = 1;
    this.getTableData();
  }

  getTableData() {
    this.modelBasePriceService.getModelBasePriceList({
      brandId: this.query.brandId,
      category: this.query.category,
      seriesId: this.query.seriesId,
      modelId: this.query.modelId,
      createTime: this.datePipe.transform(this.query.time[0], 'yyyy-MM-dd'),
      endTime: this.datePipe.transform(this.query.time[1], 'yyyy-MM-dd'),
      page: this.query.page,
      pageSize: this.query.pageSize,
    }).then((res: any) => {
      if (res.code === 0) {
        this.tableData = res.data;
      }
    });
  }

  resetCondition() {
    this.query = {
      brandId: '',
      category: '',
      seriesId: '',
      modelId: '',
      time: [],
      page: 1,
      pageSize: 10
    };
    this.seriesOptions = [];
    this.modelOptions = [];
    this.getTableData();
  }

  deleteBasePrice(id: any) {
    this.modelBasePriceService.modifyModelBasePriceList({id, deleteState: 1}).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.getTableData();
      }
    });
  }

  toModify(type: 'view' | 'modify', data: any) {
    this.router.navigate(['/aisi-recyc/modelBasePriceEdit'], {queryParams: {data: JSON.stringify(data), type}});
  }
}
